﻿@using Novagoal.Website.Models.Views
@using Novagoal.Models
@using Novagoal.Website.WebApp.Helpers
@using Novagoal.Models.Utils
@using Novagoal.Utils

@model IndexViewModel
@{
    ViewBag.Title = "Users | Index";
    Layout = "~/Views/Shared/ListLayout.cshtml";
}

@section Messages {
}

@section ListTitle {
    User List
}

@section ListActions {
    @if (Model.List.HasFilters())
    {
        @Html.ActionLink("Show All", "Index", null, new { @class = "button ui-state-default ui-corner-all" } )
    }
}

@section ListContent {

    @Html.ValidationSummary(true)

    <table class="main-table">
        <tr>
            <th class="ui-widget-header ui-corner-all id">
                @ListHelper.SortAction(Url, Model.List, "Id", UserListSortField.Id) <br />
                @ListHelper.TextBoxFor(Model.List, UserListFilter.Id, "style=\"width:60px\"")
            </th>
            <th class="ui-widget-header ui-corner-all">
                @ListHelper.SortAction(Url, Model.List, "Name", UserListSortField.Name) <br />
                @ListHelper.TextBoxFor(Model.List, UserListFilter.Name)
            </th>
            <th class="ui-widget-header ui-corner-all">
                @ListHelper.SortAction(Url, Model.List, "Email", UserListSortField.Email) <br />
                @ListHelper.TextBoxFor(Model.List, UserListFilter.Email)
            </th>
            <th class="ui-widget-header ui-corner-all">
                @ListHelper.SortAction(Url, Model.List, "Role", UserListSortField.Role) <br />
                @ListHelper.TextBoxFor(Model.List, UserListFilter.Role)
            </th>
            <th class="ui-widget-header ui-corner-all">
                @ListHelper.SortAction(Url, Model.List, "Enabled", UserListSortField.Enabled) <br />
                @ListHelper.DropDownListForBoolean(Model.List, UserListFilter.Enabled, "Enabled", "Disabled", " - Any Status - ")
            </th>
            <th class="ui-widget-header ui-corner-all date-time">
                @ListHelper.SortAction(Url, Model.List, "Last Access", UserListSortField.LastAccess) <br />
            </th>
            <th class="ui-widget-header ui-corner-all date-time">
                @ListHelper.SortAction(Url, Model.List, "Created At", UserListSortField.CreatedAt) <br />
            </th>
            @*<th class="ui-widget-header ui-corner-all date-time">
                @ListHelper.SortAction(Url, Model.List, "Updated At", UserListSortField.UpdatedAt) <br />
            </th>*@
            @if (Html.UserIsAtLeast(Enums.Roles.AdvancedUser))
            {
                <th class="ui-widget-header ui-corner-all actions" style="width:210px">Actions</th>
            }
        </tr>

        @for (int i = 0; i < Model.List.Items.Count; i++)
        {
            var status = "Disabled";
            var user = Model.List.Items[i] as User;
            if ((bool)user.Enabled)
            {
                status = "Enabled";
            }
            
            <tr id='@user.Id'>
                <td class="center">@user.Id</td>
                <td id="name-@user.Id">
                    <span id="name-span-@user.Id">@user.Person.FullName</span>
                </td>
                <td id="email-@user.Id">
                    <span id="email-span-@user.Id">@user.Person.EmailAddress</span>
                </td>
                <td id="role-@user.Id">
                    <span id="role-span-@user.Id">@user.Role.Name</span>
                </td>
                <td id="enabled-@user.Id">
                    <span id="enabled-span-@user.Id">@status</span>
                </td>
                <td>
                    @if (user.LastAccess != null)
                    {
                        @Html.Encode(String.Format("{0:g}", user.LastAccess.Value.ToLocalTime()))
                    }
                </td>
                <td>
                    @if (user.CreatedAt != null)
                    {
                        @Html.Encode(String.Format("{0:g}", user.CreatedAt.ToLocalTime()))
                    }
                </td>
                @*<td>
                    @if (user.UpdatedAt != null)
                    {
                        @Html.Encode(String.Format("{0:g}", user.UpdatedAt.ToLocalTime()))
                    }
                </td>*@
                @if (Html.UserIsAtLeast(Enums.Roles.AdvancedUser))
                {
                    <td class="center" id="edit-@user.Id">
                        @if(user.Enabled){
                            <span id="disable-button-@user.Id" onclick="ChangeUserStatus(@user.Id, false)" class="button ui-state-default ui-corner-all">Disable</span>
                        }
                        else
                        {
                            <span id="enable-button-@user.Id" onclick="ChangeUserStatus(@user.Id, true)" class="button ui-state-default ui-corner-all">Enable</span>
                        }
                        @Html.ActionLink("Edit", "Edit", "Users", new { id = user.Id }, new { @class = "button ui-state-default ui-corner-all" })
                        <span id="activation-button-@user.Id" onclick="javascript:SendActivationMail(@user.Id)" class="button ui-state-default ui-corner-all">Activation</span>
                    </td>
                }
            </tr>
        }

    </table>

    <script type="text/javascript">
        function SendActivationMail(userId) {
            var $dialog = $('<div></div>')
		    .html("Are you sure to send the activation email?")
		    .dialog({
		        autoOpen: false,
		        height: 150,
		        width: 300,
		        title: 'Confirmation',
		        modal: true,
		        buttons: {
		            Ok: function() {
	                    var url = '@Url.Action("SendActivationMail", "Users")';
                        $.post(url, { userID: userId });
                        $(this).dialog("close");
	                },
                    Cancel: function () {
		                $(this).dialog("close");
		            }
		        }
            });
            $dialog.dialog('open');
        }

        function ChangeUserStatus(userId, status) {
            var $dialog = $('<div></div>')
		    .html("Are you sure to change this user status?")
		    .dialog({
		        autoOpen: false,
		        height: 150,
		        width: 300,
		        title: 'Confirmation',
		        modal: true,
		        buttons: {
		            Ok: function () {
		                var url = '@Url.Action("ChangeUserStatus", "Users")';
		                $.post(url, { userID: userId, status: status });
		                $(this).dialog("close");
		                window.location.reload();
		            },
		            Cancel: function () {
		                $(this).dialog("close");
		            }
		        }
		    });
            $dialog.dialog('open');
        }

    </script>
}

@section ListPaging {
    @{
        @Html.DropDownListForPaging(4, 5, Model.RouteValues)
        string totalLabel = Model.List.TotalItems != 1 ? "users" : "user";
        @ListHelper.TotalItems(Model.List.TotalItems, totalLabel)
        @ListHelper.Paging(Url, Model.List)
    }
}

@section RecordEdition {
    
}


